from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import pymysql
import sys
import pickle
import database.admin.setting as setting
import interface.admin.sch_cou as sch_cou
class sch_cou_window(QMainWindow,sch_cou.Ui_Form):
    def __init__(self,cid):
        QMainWindow.__init__(self)
        sch_cou.Ui_Form.__init__(self)
        self.cid=cid
        self.setupUi(self)
        self.vol.setMaximum(200)#self.num.Value()
        self.ok.clicked.connect(self.commit)
        self.cancel.clicked.connect(self.close)
        self.read()
        self.setWindowTitle("排课:{}".format(self.cname))

    def read(self):
        with open("admin/time.pkl",'rb') as f:
            _=pickle.load(f)
            end=pickle.load(f)
            term=pickle.load(f)
        self.term_=term
        self.term.setText(term)
        for i in range(1,19):
            self.beginw.addItem("第{}周".format(i))
            self.endw.addItem("第{}周".format(i))
        for i in range(1,13,2):
            self.num.addItem("第{}、{}节".format(i,i+1))
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        sql_ = "select cname from course where id=%s"
        self.cursor.execute(sql_,self.cid)
        re = self.cursor.fetchone()
        self.cname=re[0]
        self.cname_l.setText(self.cname)
        self.cursor.close()
        self.connect.close()
    def commit(self):
        beginw = self.beginw.currentText()
        endw = self.endw.currentText()
        Mon = self.one.isChecked()
        Tue=self.two.isChecked()
        Wen=self.three.isChecked()
        Thu=self.four.isChecked()
        Fri=self.five.isChecked()
        num=self.num.currentText()
        week1=[Mon,Tue,Wen,Thu,Fri]
        week2=["星期一","星期二","星期三","星期四","星期五"]
        week=[ i[1] for i in zip(week1,week2) if i[0]==True ]
        yeartime=self.term_
        weektime=beginw+'-'+endw
        if(len(week)!=0):
            daytime=week[0]
        else:
            daytime=''
        classtime=num
        for i in range(1,len(week)):
            daytime+=','+week[i]
        volume=self.vol.value()
        info=(self.cid,yeartime,weektime,daytime,classtime,volume)
        print(info)
        if(int(beginw[1:-1])>int(endw[1:-1]) or daytime==''):
            QMessageBox.critical(self,"错误","请正确安排课程时间")
        else:
            self.connect = pymysql.connect(**setting.config)
            self.cursor = self.connect.cursor()
            sql_="Insert into course_schedule (cid,year_time,week_time,day_time,class_time,volume)values(%s,%s,%s,%s,%s,%s)"
            self.cursor.execute(sql_,info)
            self.connect.commit()
            self.cursor.close()
            self.connect.close()
            QMessageBox.information(self,"确认","{}\n始末周:{}\n{}\n节号：{}\n课容量:{}\n排课成功!".format(self.cname,weektime,daytime,classtime,volume))

if __name__=='__main__':
    app=QApplication(sys.argv)
    md=sch_cou_window('00001')
    md.show()
    sys.exit(app.exec_())